package com.cyzy.controller;

import com.cyzy.dto.ResponseDto;
import com.cyzy.util.VerificationCodeUtil;
import com.cyzy.utils.CodeUtils;
import com.cyzy.utils.RedisUtil;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import com.cyzy.pojo.Passenger;
import com.cyzy.service.UserService;
/**
 * @author cuncun
 * @create 2025-05-16 下午2:19
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private CodeUtils codeUtils;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/register")
    public ResponseDto register(@RequestBody Passenger passenger){
        if (passenger == null) {
            return ResponseDto.fail("注册数据不能为空！");
        }
        if (passenger.getPhone() == null || passenger.getPassword() == null) {
            return ResponseDto.fail("手机号和密码不能为空！");
        }
        return userService.register(passenger);
    }

    @GetMapping("/getCode")
    public ResponseDto getCode(@RequestParam("key") String key){
        //生成验证码和图片
        String codeBase64 = codeUtils.getRandomCodeImage();
        String code = codeUtils.getCode();
        //存储验证码到redis
        redisUtil.set(key, code);
        redisUtil.setExpire(key,300);
        return ResponseDto.ok(codeBase64);
    }


    @PostMapping("/login")
    public ResponseDto login(@RequestBody Passenger passenger,@RequestParam("key") String key,@RequestParam("code") String code) {
        //从redis获取验证码code
        String verification = redisUtil.get(key);
        //先判断验证码是否正确
        if(!code.equalsIgnoreCase(verification)){
            return ResponseDto.fail("验证码过期或有误！");
        }
        //获取账号密码对比数据库
        String acc=passenger.getUserAcc();
        String password=passenger.getPassword();
        if(acc==null || password==null){
            return ResponseDto.fail("账号密码不能为空！");
        }
        //返回结果集
        Map<String,Object> result=userService.login(acc,password);
        //结果集为空返回错误
        if(result==null){
            return ResponseDto.fail("账号或密码有误！");
        }
        return ResponseDto.ok(result);
    }

    //验证手机和验证码是否正确
    @GetMapping("/phoneLogin")
    public ResponseDto phoneLogin(@RequestParam("phone") String phone,@RequestParam("code") String code){

        System.out.println("phone：：" + phone + " ;  code:: " + code);
        String codeNew = redisUtil.get(phone);
        System.out.println(codeNew+"-------------------------------86");
        if(codeNew.equals(code)){
            return ResponseDto.ok("登陆成功");
        }
        return ResponseDto.fail("验证码输入错误！");
    }



    //生成短信验证码
    @GetMapping("/getPhoneCode")
    public ResponseDto getPhoneCode(@RequestParam("phone") String phone){
        String message;
        try {
            message = userService.getPhoneCode(phone);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return ResponseDto.ok(message);
    }

    //忘记密码重新设置密码
    @GetMapping("/forgotPassword")
    public ResponseDto forgotPassword(@RequestParam("code")String code,@RequestParam("phone")String phone,@RequestParam("userId")long userId,@RequestParam("password")String password){

//        System.out.println(userId + " ---------userId");
//        System.out.println(password + " ----password");

        if(userId == 0 || password == null || code == null || phone == null){
            return ResponseDto.fail("参数不能为空！");
        }
        String codeNew = redisUtil.get(phone);
        if(!codeNew.equals(code)){
            return ResponseDto.fail("验证码错误！");
        }
        userService.forgotPassword(userId,password);
        return ResponseDto.ok("重置成功");
    }

    //退出
    @GetMapping("/logout")
    public ResponseDto logout(@RequestParam("token") String token){
        if(token==null){
            return ResponseDto.ok("token为空！");
        }
        //删除token
        if(!redisUtil.del(token)){
            return ResponseDto.fail("删除token失败！");
        }
        return ResponseDto.ok("删除token成功！");
    }

    //getUserById
    @GetMapping("/getUser")
    public ResponseDto getUser(@RequestParam("userId") Integer userId){
        Passenger user = userService.getById(userId);
        if(user == null){
            return ResponseDto.fail();
        }
        return ResponseDto.ok(user);
    }
}
